package com.pocketsweet;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;

import com.pocketsweet.model.MLArticle;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.utils.ShareReferanceUtils;

public class MLContext {
	private static MLContext self;
	private static ShareReferanceUtils mPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "LIKE");
	private static ShareReferanceUtils userPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "USER_LIKE");
	private static ShareReferanceUtils messagePreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "MESSAGE_RECORD");
	private static ShareReferanceUtils loginPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "USER");
	private static ShareReferanceUtils taskPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "TASK");
	private static ShareReferanceUtils datePreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "DATE");
	private static ShareReferanceUtils loverTipPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "LOVER_TIP");
	private static ShareReferanceUtils loverCallTipPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "LOVER_CALL_TIP");

	private static ShareReferanceUtils exceptionPreferences = new ShareReferanceUtils(
			MLApplication.getContext(), "EXCEPTION");
	public Context mContext;
	private static List<MLUser> mFriends = new ArrayList<MLUser>();
	private static List<MLArticle> mArticle = new ArrayList<MLArticle>();
	public static final String LIKE_TYPE_USER = "like_type_user";
	public static final String LIKE_TYPE_RECOMMEND = "like_type_recommend";
	public static final String TASK_INVITE_FRIEND = "task_invite_friend";// 邀请好友
	public static final String TASK_CONSUMMATE = "task_consummate";// 完善资料

	public static boolean isTestOrderToday = false;

	private MLContext() {
	}

	private MLContext(Context context) {
		mContext = context;
		self = this;
	}

	public static MLContext getInstance() {
		return self;
	}

	public static void init(Context context) {
		self = new MLContext(context);
	}

	public List<MLUser> getFridendList() {
		return mFriends;
	}

	public void setFriendsList(List<MLUser> fridens) {
		mFriends = fridens;
	}

	public static List<MLArticle> getmArticle() {
		return mArticle;
	}

	public static void setmArticle(List<MLArticle> mArticle) {
		MLContext.mArticle = mArticle;
	}

	// public static void setUserLikeID(String userId, List<String> userIDList)
	// {
	// mPreferences = MLApplication.getContext().getSharedPreferences(
	// "USER_LIKE", Activity.MODE_PRIVATE);
	// SharedPreferences.Editor editor = mPreferences.edit();
	// editor.putString("userID", userIDList.toString());
	// editor.commit();
	// }

	public static int getExceotionUserMoney() {
		return exceptionPreferences.getIntValue("EXCEPTION", 0);
	}

	public static void saveExceotionUserMoney(int money) {
		exceptionPreferences.setIntValue("EXCEPTION", money);
	}

	/*
	 * 查询sharepreference是否完成了恋爱体验师的提示
	 */
	public static int getLoverTip() {
		return loverTipPreferences.getIntValue("LOVER_TIP", 0);
	}

	public static int getLoverCallTip() {
		return loverCallTipPreferences.getIntValue("LOVER_CALL_TIP", 0);
	}

	public static void setLoverTip() {
		loverTipPreferences.setIntValue("LOVER_TIP", 1);
	}

	public static void setLoverCallTip() {
		loverCallTipPreferences.setIntValue("LOVER_CALL_TIP", 1);
	}

	/*
	 * 查询sharepreference是否有喜欢该对象
	 */
	public static int getUserLikeID(String userID) {
		return mPreferences.getIntValue("userID_" + userID, 0);
	}

	public static int getArticleLikeID(String articleID) {
		return mPreferences.getIntValue("articleID_" + articleID, 0);
	}

	/*
	 * 查询sharepreference是否有纪录
	 */
	public static int getUserLikeSize(String userSize) {
		return mPreferences.getIntValue(userSize, -1);
	}

	public void setUserLikeSizeZero() {
		mPreferences.setIntValue("userSize", 0);
	}

	public static int getArticleLikeSize(String articleSize) {
		return mPreferences.getIntValue(articleSize, -1);
	}

	public void setAticleLikeSizeZero() {
		mPreferences.setIntValue("articleSize", 0);
	}

	/*
	 * 查询sharepreference是否有纪录
	 */
	public static String getUserLogin(String message) {
		return loginPreferences.getStringValue(message, "");
	}

	/*
	 * 查询sharepreference是否有纪录
	 */
	public static int getTask(String task) {
		return taskPreferences.getIntValue(task, -1);
	}

	/*
	 * 把喜欢该对象保存到sharepreference
	 */
	public static void saveUserLikeID(String userID) {
		mPreferences.setIntValue("userID_" + userID, 1);
		int userSize = mPreferences.getIntValue("userSize", 0);
		userSize++;
		mPreferences.setIntValue("userSize", userSize);
	}

	public static void saveArticleLikeID(String articleID) {
		mPreferences.setIntValue("articleID_" + articleID, 1);
		int articleSize = mPreferences.getIntValue("articleSize", 0);
		articleSize++;
		mPreferences.setIntValue("articleSize", articleSize);
	}

	public static void saveTask(String task, int value) {
		taskPreferences.setIntValue(task, value);
	}

	/*
	 * 把喜欢该对象从sharepreference删除
	 */
	public static void deleteUserLikeID(String userID) {
		if (getUserLikeID(userID) == 0) {
			return;
		}
		int userSize = mPreferences.getIntValue("userSize", 0);
		userSize--;
		mPreferences.setIntValue("userSize", userSize);
		mPreferences.remove("userID_" + userID);
	}

	public static void deleteArticleLikeID(String articleID) {
		if (getArticleLikeID(articleID) == 0) {
			return;
		}
		int articleSize = mPreferences.getIntValue("articleSize", 0);
		articleSize--;
		mPreferences.setIntValue("articleSize", articleSize);
		mPreferences.remove("articleID_" + articleID);
	}

	/*
	 * 把喜欢该对象从sharepreference删除
	 */
	public static void deleteAll() {
		mPreferences.removeAll();
		userPreferences.removeAll();
		messagePreferences.removeAll();
		datePreferences.removeAll();
		loginPreferences.removeAll();
		taskPreferences.removeAll();
		exceptionPreferences.removeAll();
	}

	/*
	 * 保存喜欢的人或推荐
	 */
	public static void saveLike(String type, String likeID) {
		int userSize = userPreferences.getIntValue(type, 0);
		userSize++;
		userPreferences.setStringValue(type + userSize, likeID);
		userPreferences.setIntValue(type, userSize);
		// Log.e("=save",
		// "userPreferences:" + likeID + " and "
		// + userPreferences.getStringValue(type + userSize, null)
		// + "   "
		// + String.valueOf(userPreferences.getIntValue(type, 0)));
	}

	/*
	 * 得到所有喜欢的人或推荐
	 */
	public static ArrayList<String> getLike(String type) {
		ArrayList<String> idList = new ArrayList<String>();
		int size = userPreferences.getIntValue(type, 0);
		String id;
		if (size > 0) {
			for (int i = 1; i <= size; i++) {
				id = userPreferences.getStringValue(type + i, null);
				// Log.e("=get", "userPreferences:" + id + " ");
				idList.add(id);
			}
		}
		return idList;
	}

	public static void deleteLike(String type, String likeID) {
		ArrayList<String> idList = getLike(type);
		for (int i = 1; i <= idList.size(); i++) {
			if (likeID.equals(idList.get(i - 1))) {
				userPreferences.setStringValue(type + i, null);
				// Log.e("=delete", "userPreferences:" + likeID + " and "
				// + userPreferences.getStringValue(type + i, null));
				break;
			}
		}
	}

	/*
	 * 保存消息设置
	 */
	public static void saveMessage(String message, boolean isRemind) {
		messagePreferences.setBooleanValue(message, isRemind);
	}

	public static void saveMessage(String message, int time) {
		messagePreferences.setIntValue(message, time);
	}

	public static void saveForbiddenTime(String message, String date) {
		datePreferences.setStringValue(message, date);
	}

	public static String getForbiddenTime(String message) {
		return datePreferences.getStringValue(message, "");
	}

	/*
	 * 查询sharepreference是否有纪录
	 */
	public static boolean getMessage(String message) {
		if (message.equals("message_border")) {
			return messagePreferences.getBooleanValue(message, false);
		} else {
			return messagePreferences.getBooleanValue(message, true);
		}
	}

	public static int getBorederTimeMessage(String message) {
		if (message.equals("message_border_start")) {
			return messagePreferences.getIntValue(message, 23);
		} else if (message.equals("message_border_end")) {
			return messagePreferences.getIntValue(message, 8);
		}
		return 0;
	}

}
